package excel;

import string.UtilString;


import java.util.HashMap;
import java.util.Map;

/**
 * Description: coordinate cell address,convert  A1 to (0,0), or reserve.
 * Created by 4790K_NSL on 2017/4/29.
 */
public class UtilExcelCoordinateCellAddress {
    //{<A:0>,<B:1>....>
    static private Map<String,Integer> letterMap=new HashMap<>();
    static {
        for (int i = 65; i <91 ; i++) {
            char letter= (char) i;
           // System.out.println(letter);
            letterMap.put(letter+"",i-65);
        }
        //System.out.println(letterMap);
    }

    public static void main(String[] args) {


    }

    /**
     * 根据基准点和偏移量，得到偏移后的点位置
     * @param origin
     * @param offset
     * @return
     */
    static public  ExcelPoint getDestinationAfterOffset(ExcelPoint origin,ExcelPoint offset){
        int x=origin.getX()-offset.getX();
        int y=origin.getY()-offset.getY();
        if(x<0 || y<0){
            throw new RuntimeException("offset point error.");
        }
        return new ExcelPoint(x,y);
    }


    /**
     * convert A1 -> (0,0)
     * @param cellAddress
     * @return
     */
    static public  ExcelPoint convertFromCellAddress(String cellAddress){
        ExcelPoint point=new ExcelPoint(-1,-1);
        if(UtilString.isNullOrEmpty(cellAddress)){
            throw new RuntimeException("cellAddres in null.");
        }

        cellAddress=cellAddress.toUpperCase();
        if(!cellAddress.matches("[A-Z]+[1-9]+[0-9]*")){
            throw new RuntimeException("cellAddress in illegal.");
        }
        String columnString=cellAddress.replaceAll("\\d+","");
        String rowString=cellAddress.replaceAll("\\D+","");
        //System.out.println(columnString);
        //System.out.println(rowString);
        int y=convertColumnString2ColumnIndex(columnString);
        int x=Integer.parseInt(rowString)-1;
        point.setLocation(x,y);
        return  point;
    }

    /** 使用递归的方式，转换列坐标： A->0 , AB->27
     * @param columnString
     * @param lastRound
     * @return
     */
    static private int convertColumnString2ColumnIndex(String columnString,int lastRound){
        if(columnString.length()>1){
           String highLetter=columnString.charAt(0)+"" ;
           String rightLetters=columnString.substring(1);
           int high=(letterMap.get(highLetter)+1)+lastRound*26;
            //System.out.println(rightLetters);
            //System.out.println(high);
            return convertColumnString2ColumnIndex(rightLetters,high);

        }else {
            return letterMap.get(columnString)+lastRound*26;
        }
    }


    /**
     * @param columnString  列坐标: A or AB
     * @return
     */
    static public int convertColumnString2ColumnIndex(String columnString){
        if (UtilString.isNullOrEmpty(columnString)) {
            throw new RuntimeException(columnString+" is null or empty.");
        }
        columnString=columnString.toUpperCase();
        return  convertColumnString2ColumnIndex(columnString,0);
    }


}
